package com.itsu.integration.service.impl;

import com.itsu.integration.service.MsgEndPoint;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;

/**
 * @author suben
 * @date 2020/4/29 23:01
 */
@Service
public class MsgEndPointImpl implements MsgEndPoint {


  /*  @Resource
    private RequestGateWay requestGateWay;
*/
    @Override
    public Message sendMessage(Message request) throws Exception {
        //模拟异常发生
//        int num = new Random().nextInt(10);
//        if (num > 0 && num <= 3) {
//            num = 1 / 0;
//        } else if (num > 3 && num < 7) {
//            throw new CustomException("自定义异常");
//        } else {
//            System.out.println("本次模拟没有异常发生");
//        }

//        Message obj = requestGateWay.sendAndReceive(request);
        Message obj = request;
        return obj;
    }


//    private Message callSoap(Message inMessage) {
//        WebServiceTemplate soapClient = new WebServiceTemplate();
//        soapClient.setMarshaller(marshaller());
//        soapClient.setUnmarshaller(marshaller());
//
//        String payloadStr = inMessage.getPayload().toString();
//        GetCountryRequest request = new GetCountryRequest();
//        Message<?> message = MessageBuilder.withPayload("").build();
//
//        try {
//
//            //json convert to soap
//            //System.out.println("xXXXX  build soap request XXXXXXXXXXXXX" +payloadStr);
//            request = (GetCountryRequest) jsonToObj(new GetCountryRequest(), payloadStr);
//
//            //System.out.println("xXXXX  marshalSendAndReceive  requexst.get value is "+ request.getName());
//            GetCountryResponse response = (GetCountryResponse) soapClient.marshalSendAndReceive("http://localhost:8111/ws/countries", request,
//                    new SoapActionCallback("http://spring.io/guides/gs-producing-web-service/GetCountryRequest"));
//
//            //System.out.println("======================"+response.getCountry().getCurrency());
//            String strResponse = objToJson(response);
//
//            message = MessageBuilder.withPayload(strResponse).build();
//        } catch (Exception e) {
//            System.out.println("XXXX " + e.getMessage());
//        } finally {
//            return message;
//        }
//    }
//
//    public Jaxb2Marshaller marshaller() {
//        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
//        // this package must match the package in the <generatePackage> specified in
//        // pom.xml
//        marshaller.setContextPath("com.itsu.integration.factory");
//        return marshaller;
//    }
//
//    public Object jsonToObj(Object obj, String jsonStr) throws JsonParseException, JsonMappingException, IOException {
//        ObjectMapper mapper = new ObjectMapper();
//        return obj = mapper.readValue(jsonStr, obj.getClass());
//    }
//
//    public String objToJson(Object obj) throws JsonProcessingException {
//        ObjectMapper mapper = new ObjectMapper();
//        return mapper.writeValueAsString(obj);
//    }
}
